#include "umlview.h"

UmlView::UmlView(QWidget *parent) : QGraphicsView(parent)
{
    // recruiting OpengGL widget for drawing

    if (QGLFormat::hasOpenGL()) {
        this->setViewport(new QGLWidget(QGLFormat(QGL::SampleBuffers)));
        qDebug() << "system supports OpenGL";
    }

    this->setDragMode(QGraphicsView::RubberBandDrag);

    QPrinter printer;
    this->setSceneRect(printer.paperRect(QPrinter::Point));

    this->_zoomFactor = 1.2;
    this->_zoomLevel = 0;

    this->resetScene();
}

void UmlView::setPrinter(QPrinter *printer)
{
    this->_printer = printer;
}

void UmlView::zoomIn()
{
     this->scale(this->_zoomFactor, this->_zoomFactor);
     this->_zoomLevel++;
}

void UmlView::zoomOut()
{
     this->scale(1.0 / this->_zoomFactor, 1.0 / this->_zoomFactor);
     this->_zoomLevel--;
}


void UmlView::zoomReset()
{
    if (this->_zoomLevel > 0) {
        for (int i = 0; i < this->_zoomLevel; i++) {
            this->scale(1.0 / this->_zoomFactor, 1.0 / this->_zoomFactor);
        }
    }

    if (this->_zoomLevel < 0) {
        for (int i = 0; i > this->_zoomLevel; i--) {
            this->scale(this->_zoomFactor, this->_zoomFactor);
        }
    }

    this->_zoomLevel = 0;
}

void UmlView::resetScene()
{
    this->setScene(&this->_emptyScene);
}
